import javaRequest from "@/service/javaInterface.js";
const Books = {
  namespaced: true,
  state: {
    booksList: [],
    booksCount: 0,
  },
  getters: {},
  mutations: {
    changebooksList(state, booksList) {
      state.booksList = booksList;
    },
    changebooksListTotal(state, count) {
      state.booksCount = count;
    },
  },
  actions: {
    async getbooksTotal({ commit }, payload) {
      const { pageUrl } = payload;
      const result = await javaRequest.request({
        url: "/books",
        method: "GET",
      });
      commit("changebooksListTotal", result.data.data.length);
    },
    async getbooksPageList({ commit }, payload) {
      const { pageUrl, queryInfo } = payload;
      const { offset, size } = queryInfo;
      var num = queryInfo.offset;
      if (pageUrl == "/getbooksList") {
        let offset = num.toString();
        if (offset == 0) {
          num = 1;
        }
        if (offset.length >= 2) {
          var newOffset = offset.substring(0, 1);
          num = parseInt(newOffset) + 1;
          console.log(num);
        }
        if (offset.length >= 3) {
          var newOffset = offset.substring(0, 2);
          num = parseInt(newOffset) + 1;
        }
      }

      const result = await javaRequest.request({
        url: `/books/${num}/${size}`,
        method: "GET",
      });
      commit("changebooksList", result.data.data.records);
    },
  },
};

export default Books;
